package com.example.mvpdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.mvpdemo.model.Callback;
import com.example.mvpdemo.model.MainModel;
import com.example.mvpdemo.model.User;
import com.example.mvpdemo.presenter.IPresenter;
import com.example.mvpdemo.presenter.MainPresenter;

public class MainActivity extends AppCompatActivity implements IView{

    private EditText et_username;
    private EditText et_password;
    private Button btn_login;
    private ProgressDialog progressDialog;
    private IPresenter mainPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);

        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("请稍后...");

        mainPresenter = new MainPresenter(this);
    }

    public void onLogin(View view) {
        String username = et_username.getText().toString();
        String password = et_password.getText().toString();
        progressDialog.show();

        mainPresenter.login(username,password);
    }

    @Override
    public void showPregress() {
        progressDialog.show();
    }

    @Override
    public void hidePregress() {
        progressDialog.dismiss();
    }

    @Override
    public void loginSucess(User user) {
        Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginFailure(String msg) {
        Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show();

    }
}